package demo;

import com.mysql.cj.api.result.Row;
import demo.model.Book;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.Assert;

/**
 * Created by chenx on 2017/7/23.
 */
@SpringBootApplication
public class Application implements CommandLineRunner {

  private JdbcTemplate jdbcTemplate;

  @Autowired
  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    Assert.notNull(jdbcTemplate, "jdbc template is not null");
    this.jdbcTemplate = jdbcTemplate;
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

  @Override
  public void run(String... strings) throws Exception {
    Book book = jdbcTemplate
        .queryForObject("select  * from book where id = ?", new Object[]{"S130000--1100"},
            new RowMapper<Book>() {
              @Override
              public Book mapRow(ResultSet resultSet, int i) throws SQLException {
                Book book = new Book();
                book.setId(resultSet.getString("id"));
                book.setIsdn(resultSet.getString("isdn"));
                book.setTitle(resultSet.getString("title"));
                return book;
              }
            });

    System.out.println(book);
  }


}
